<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Form
 * @subpackage Decorator
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
/** Zend_Form_Decorator_Abstract */
require_once 'Zend/Form/Decorator/Abstract.php';

/** Zend_Form_Decorator_Marker_File_Interface */
require_once 'Zend/Form/Decorator/Marker/File/Interface.php';

/** Zend_File_Transfer_Adapter_Http */
require_once 'Zend/File/Transfer/Adapter/Http.php';

/**
 * Zend_Form_Decorator_File
 *
 * Fixes the rendering for all subform and multi file elements
 *
 * @category   Zend
 * @package    Zend_Form
 * @subpackage Decorator
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: File.php 23775 2011-03-01 17:25:24Z ralph $
 */
class My_Form_Decorator_ImageUpload extends Zend_Form_Decorator_File {

    /**
     * Render a form file
     *
     * @param  string $content
     * @return string
     */
    public function render($content) {
        $element = $this->getElement();
        $view = $element->getView();
//        $view->headLink()->appendStylesheet($view->baseUrl() . '/libs/fileupload/css/jquery.fileupload-ui.css');
        $view->headLink()->appendStylesheet($view->baseUrl() . '/css/form/image-upload.css');
        $view->headScript()->appendFile($view->baseUrl() . '/libs/fileupload/js/jquery.iframe-transport.js');
        $view->headScript()->appendFile($view->baseUrl() . '/libs/fileupload/js/jquery.fileupload.js');

        $html = parent::render($content);

        $value = $element->getValue();
        $image = null;
        if(!empty($value) && $value > 0) {
            $daoImage = new Application_Model_DAO_Image();
            $image = $daoImage->get($value);
        }

        $view->placeholder('html')->captureStart();
        ?>

        <div id="<?= $element->getId() ?>_container" class="image-upload-container">
            <span class="imageUploadBtn btn btn-primary">
                <span>Prześlij zdjęcie</span>
                <?= $html ?>
            </span>
            <div class="thumbnail imagePreview" style="<?= empty($image) ? '' : 'display: block;'?>">
                <img class="preview" src="<?= (empty($image) ? '' : $view->url(array('id' => $image['code'], 'width' => 60, 'height' => 40), 'image', true)) ?>" alt="asd" />
                <button class="removeBtn btn btn-mini">Usuń zdjęcie</button>
            </div>
            <input type="hidden" class="remove" name="<?= $element->getId() ?>_remove" value="0" />
            <input type="hidden" class="code" name="<?= $element->getId() ?>_code" value="<?= empty($image) ? '' : $image['code'] ?>" />
        </div>
        <script type="text/javascript">
            var kam = null;
            $('#<?= $element->getId() ?>_container #<?= $element->getId() ?>').on('change', null, function(e) {
                var container = $(this).parents('.image-upload-container');
                var file = this.files[0];
                var fr = new FileReader();
                fr.onload = function (e) {
                    $('.preview', container).attr('src', e.target.result);
                    $('.imagePreview', container).slideDown();
//                    $('.remove', container).val('0');
                }
                fr.readAsDataURL(file);
            });
            $('#<?= $element->getId() ?>_container .removeBtn').on('click', null, function(e) {
                e.preventDefault();
                var container = $(this).parents('.image-upload-container');
                $('.remove', container).val('1');
                $('.imagePreview', container).slideUp();
            });
        </script>

        <?php
        $view->placeholder('html')->captureEnd();

        $html = $view->placeholder('html');

        return $html;
    }

}
